//
//  CZVisitor.swift
//  项目框架
//
//  Created by yang on 16/2/25.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import SnapKit

/*
1.定义协议用 protocol
2.协议继承自 NSObjectProtocol
3.用 weak 来修饰代理属性
*/

protocol CZVisitorDelegate: NSObjectProtocol{

    //定义方法
    func didLogin()
    func didRegister()
}
class CZVisitor: UIView {

    //设置代理属性
    weak var delegate:CZVisitorDelegate?
    
    
    
   //自定义三部曲
    //1  重写init
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        //设置背景色为白色
        backgroundColor = UIColor(white: 237/255.0, alpha: 1)
        
        setUI()

    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //2 私有方法
    private func setUI(){
    
        //添加控件
        addSubview(homeIconImageView)
        //添加遮罩视图
        addSubview(maskImageView)
        addSubview(iconImageView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        //添加约束
        //动画居中对齐
        homeIconImageView.snp_makeConstraints{(make) -> Void in
        
            make.center.equalTo(self.snp_center)
        }
        iconImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.snp_center)
        }
        //添加label
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconImageView.snp_bottom).offset(10)
            
            make.centerX.equalTo(self.snp_centerX)
            
            make.width.equalTo(216)
        }
        
        //注册按钮
        /*
        距离左边是 -messageLabel的左边
        上边是 messageLabel的底部
        宽100 高30
        */
        registerButton.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(messageLabel.snp_bottom).offset(10)
            
            make.left.equalTo(messageLabel.snp_left)
            
            make.width.equalTo(100)
            
            make.height.equalTo(30)
        }
        
        loginButton.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(messageLabel.snp_right)
            make.top.equalTo(messageLabel.snp_bottom).offset(10)
            
            make.width.equalTo(100)
            make.height.equalTo(30)
        }
        
        //添加遮罩视图的约束
        maskImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.snp_center)
        }
    }

        
    //添加动画
    func startAnimation(){
    
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        
        animation.toValue = 2 * M_PI
        
        animation.repeatCount = MAXFLOAT
        
        animation.duration = 5
        //切换视图的时候,保证动画不要被移除
        animation.removedOnCompletion = false
        
        //添加到
        homeIconImageView.layer.addAnimation(animation, forKey: nil)
        
    }
    
    //对外提供一个放法来修改message，动画还有图片
    func setInfo(message:String,image:String,animation:Bool){
    
        messageLabel.text = message
        
        if animation{
        
            homeIconImageView.hidden=false
            
            startAnimation()
        }else{
        
            homeIconImageView.hidden=true
        }
    }
    
    //3 懒加载
    
    //首页动画图片
    private lazy var homeIconImageView:UIImageView={
    
        let img = UIImageView()
        
        img.image = UIImage(named: "visitordiscover_feed_image_smallicon")
        
        return img
    }()
    
    //通用的图片
    private lazy var iconImageView:UIImageView={
    
        let img = UIImageView()
        
        img.image = UIImage(named: "visitordiscover_feed_image_house")
        
        return img
    
    }()
    //label
    private lazy var messageLabel:UILabel={
    
        let label = UILabel()
        
        label.font = UIFont.systemFontOfSize(14)
        
        label.textColor = UIColor.darkGrayColor()
        
        label.numberOfLines = 0
        
        label.textAlignment = .Center
        
        label.text = "关注一些人，回这里看看有什么惊喜"
        
        return label
    }()
    
    //注册按钮
    private lazy var registerButton:UIButton={
    
        let button = UIButton(type: UIButtonType.Custom)
        
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        button.setTitle("注册", forState: UIControlState.Normal)
        
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        button.addTarget(self, action: "register", forControlEvents: UIControlEvents.TouchUpInside)
        
        
        return button
    
    
    }()
    
    //登录按钮
    
    private lazy var loginButton: UIButton = {
        
        let button = UIButton(type: UIButtonType.Custom)
        
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        button.setTitle("登陆", forState: UIControlState.Normal)
        
        button.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        
        button.addTarget(self, action: "login", forControlEvents: UIControlEvents.TouchUpInside)
        
        
        return button
        
    }()
    
    //按钮的代理方法
    func login(){
        delegate?.didLogin()
    }
    
    func register(){
    
        delegate?.didRegister()
    }
    
    //遮罩视图
    //遮罩视图
    private lazy var maskImageView: UIImageView = {
        
        let img = UIImageView()
        
        img.image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        
        return img
    }()


}
